package com.yougou.ui.order.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yougou.R;
import com.yougou.data.model.Order;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {
    
    private List<Order> mDataList = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;
    
    public void setData(List<Order> dataList) {
        if (dataList != null) {
            mDataList = dataList;
            notifyDataSetChanged();
        }
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    
    public interface OnItemClickListener {
        void onItemClick(Order order);
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_order, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Order order = mDataList.get(position);
        
        holder.tvOrderNumber.setText("订单号：" + order.getOrderNumber());
        holder.tvOrderStatus.setText(order.getStatusText());
        holder.tvOrderPrice.setText(String.format(Locale.CHINA, "¥%.2f", order.getOrderPrice()));
        
        // 格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        holder.tvCreateTime.setText(sdf.format(new Date(order.getCreateTime() * 1000)));
        
        // 显示商品数量
        if (order.getGoodsList() != null && !order.getGoodsList().isEmpty()) {
            int totalCount = 0;
            for (Order.OrderGoods goods : order.getGoodsList()) {
                totalCount += goods.getGoodsNumber();
            }
            holder.tvGoodsCount.setText("共" + totalCount + "件商品");
        }
        
        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(order);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvOrderNumber;
        TextView tvOrderStatus;
        TextView tvOrderPrice;
        TextView tvCreateTime;
        TextView tvGoodsCount;
        
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvOrderNumber = itemView.findViewById(R.id.tv_order_number);
            tvOrderStatus = itemView.findViewById(R.id.tv_order_status);
            tvOrderPrice = itemView.findViewById(R.id.tv_order_price);
            tvCreateTime = itemView.findViewById(R.id.tv_create_time);
            tvGoodsCount = itemView.findViewById(R.id.tv_goods_count);
        }
    }
}

